package com.application.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.UUID;
import com.application.dao.UserDao;
import com.application.domain.User;
import com.application.dto.UserDTO;
import com.application.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.application.util.RedisConstant.LOGIN_USER_TTL;
import static com.application.util.RedisConstant.USER_LOGIN_KEY;

@Service
//这里是 service层  只是  用于  controller和dao之间的过度   返回信息和获取客户信息都在  controller层
public class UserServiceImpl implements UserService {
    @Resource
    UserDao userDao;

    @Resource
    private StringRedisTemplate stringRedisTemplate;


    @Override
    @Transactional
    public int save(User user) {
        //从页面获取的user  传进去
        int insert = userDao.insert(user);
        return insert;
    }

    @Override
    @Transactional
    public int deleteById(String openid) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getOpenid,openid);
        int deleteById = userDao.delete(lqw);
        return deleteById;
    }

    @Override
    @Transactional
    public int deleteByIds(List<String> ids) {
        int deleteBatchIds = userDao.deleteBatchIds(ids);
        return deleteBatchIds;
    }

    @Override
    @Transactional
    public int deleteAll() {
        int delete = userDao.delete(null);
        return delete;
    }

    @Override
    @Transactional
    public int updateById(User user) {
        if("负责人".equals(user.getPosition())){
            user.setLevel(2);
        }
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getOpenid,user.getOpenid());
        int update = userDao.update(user,lqw);
        return update;
    }

    @Override
    @Transactional
    public User selectById(String openid) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getOpenid,openid);
        User user = userDao.selectOne(lqw);
//        User user = userDao.selectByOpenidUser(openid);
        return user;
    }

    @Override
    @Transactional
    public List<User> selectByIds(List<String>ids) {
        List<User> users = userDao.selectBatchIds(ids);
        return users;
    }

    @Override
    @Transactional
    public List<User> selectAll() {
        List<User> users = userDao.selectList(null);
        return users;
    }

    @Override
    @Transactional
    public String Login(User user) {
        // 这里就是 接收  用户信息 进行判断
        // 这里的登录就需要进行改变  需要将 数据库中的数据  保存到redis当中
        User user1 = getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, user.getUsername()).eq(User::getPassword, user.getPassword()));
        // 说明这里就是 没有任何对应的用户
        if(user1 == null){
            return null;
        }
        // 这里就是 生成 每个用户 对应唯一的  token  进行放到 redis中 作为key
        String token = UUID.randomUUID(true).toString();
        String key = USER_LOGIN_KEY + token;
        System.err.println(user1);
        // 保存到 保护类当中
        UserDTO userDTO = BeanUtil.toBean(user1, UserDTO.class);
        Map<String,Object> userDTOMap = BeanUtil.beanToMap(userDTO, new HashMap<>(),
                CopyOptions.create()
                        // 忽略空值
                        .setIgnoreNullValue(true)
                        // 这里因为 user实体类中 不是所有都是 string类型  所有 需要将 所有类型先转为 string类型再进行存储
                        .setFieldValueEditor((fieldName, fieldValue) -> fieldValue.toString()));
        // 在redis当中  我们  使用  hash来存储
        stringRedisTemplate.opsForHash().putAll(key,userDTOMap);
        // 然后 设置 用户TTL时间
        stringRedisTemplate.expire(key,LOGIN_USER_TTL, TimeUnit.MINUTES);

        return token;
    }

    @Override
    @Transactional
    /**
     * true   就是代表  可以进行注册  没有用户名重复
     */
    public int register(User user) {
        User user1= userDao.selectByUsername(user);
        int insert=0;
        //  就是   没有注册用户
        if (user1==null){
            insert=userDao.insert(user);
            User user2 = userDao.selectByUsername(user);
        }
        return insert;
    }


    @Override
    public int DownLoad(User user) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getOpenid,user.getOpenid());
        int updateById = userDao.update(user,lqw);
        return updateById;
    }

    @Override
    public String getId(User user) {
        User user1 = userDao.selectByUsername(user);
        return user1.getId();
    }

    @Override
    public User getOne(LambdaQueryWrapper<User> lqw) {
        User user = userDao.selectOne(lqw);
        return user;
    }

    @Override
    public List<User> getMemberByName(String name) {
        List<User> memberByName = userDao.getMemberByName(name);
        return memberByName;
    }

    @Override
    public int updateByLeader(String com_name, String position, String openid) {
        int updateByLeader = userDao.updateByLeader(com_name, position, openid);
        return updateByLeader;
    }

    @Override
    public int updateFlagByOpenid(String openid) {
        int updateFlagByOpenid = userDao.updateFlagByOpenid(openid);
        return updateFlagByOpenid;
    }

    @Override
    public String getSchool(String openid) {
        String school = userDao.getSchool(openid);
        return school;
    }


}
